{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "# This kernel reproduces this issue:\n",
    "# https://github.com/bukosabino/ta/issues/25\n",
    "# Based on this example:\n",
    "# https://stockcharts.com/school/doku.php?id=chart_school:technical_indicators:average_true_range_atr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [],
   "source": [
    "def average_true_range(high, low, close, n=14, fillna=False):\n",
    "    \"\"\"Average True Range (ATR)\n",
    "\n",
    "    The indicator provide an indication of the degree of price volatility.\n",
    "    Strong moves, in either direction, are often accompanied by large ranges,\n",
    "    or large True Ranges.\n",
    "\n",
    "    http://stockcharts.com/school/doku.php?id=chart_school:technical_indicators:average_true_range_atr\n",
    "\n",
    "    Args:\n",
    "        high(pandas.Series): dataset 'High' column.\n",
    "        low(pandas.Series): dataset 'Low' column.\n",
    "        close(pandas.Series): dataset 'Close' column.\n",
    "        n(int): n period.\n",
    "\n",
    "    Returns:\n",
    "        pandas.Series: New feature generated.\n",
    "    \"\"\"\n",
    "\n",
    "    # tr = np.maximum(np.array(abs(df.high - df.low)), np.array(abs(df.high - df.close.shift(1))), np.array(abs(df.low - df.close.shift(1))))\n",
    "    cs = close.shift(1)\n",
    "    tr = high.combine(cs, max) - low.combine(cs, min)\n",
    "\n",
    "    atr = np.zeros(len(close))\n",
    "    atr[0] = tr[1::].mean()\n",
    "    for i in range(1, len(atr)):\n",
    "        atr[i] = (atr[i-1] * (n-1) + tr[i]) / n\n",
    "\n",
    "    if fillna:\n",
    "        atr = atr.replace([np.inf, -np.inf], np.nan).fillna(0)\n",
    "\n",
    "    return pd.Series(atr, name='atr')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [],
   "source": [
    "high_data = [48.70, 48.72, 48.90, 48.87, 48.82, 49.05, 49.20, 49.35, 49.92, 50.19, 50.12, 49.66, 49.88, 50.19, 50.36, 50.57, 50.65, 50.43]\n",
    "low_data = [47.79, 48.14, 48.39, 48.37, 48.24, 48.64, 48.94, 48.86, 49.50, 49.87, 49.20, 48.90, 49.43, 49.73, 49.26, 50.09, 50.30, 49.21]\n",
    "close_data = [48.16, 48.61, 48.75, 48.63, 48.74, 49.03, 49.07, 49.32, 49.91, 50.13, 49.53, 49.50, 49.75, 50.03, 50.31, 50.52, 50.41, 49.34]\n",
    "\n",
    "df = pd.DataFrame()\n",
    "\n",
    "df['high'] = high_data\n",
    "df['low'] = low_data\n",
    "df['close'] = close_data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0     0.588235\n",
       "1     0.587647\n",
       "2     0.582101\n",
       "3     0.576236\n",
       "4     0.576505\n",
       "5     0.564612\n",
       "6     0.542854\n",
       "7     0.539079\n",
       "8     0.543430\n",
       "9     0.527471\n",
       "10    0.556223\n",
       "11    0.570779\n",
       "12    0.562152\n",
       "13    0.554855\n",
       "14    0.593794\n",
       "15    0.585666\n",
       "16    0.568833\n",
       "17    0.615344\n",
       "Name: atr, dtype: float64"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "average_true_range(df['high'], df['low'], df['close'], n=14, fillna=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
